//////////////////////////////////////////////////////////////////////////////////
//	Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include <wx/filename.h>
#include <wx/file.h>

#include "logsaver.h"
#include "guilogic.h"

CLogSaver::CLogSaver(CGUILogic& guiLogic):m_guiLogic(guiLogic)
{
}

CLogSaver::~CLogSaver(void)
{
}

void CLogSaver::AddLogRecord(const wxString& strLogRec)
{
	if ( IsSaveEnabled() )
	{
		wxString strFileName;

		try
		{
			wxFile::OpenMode mode(wxFile::write_append);
			strFileName =  GenerateFileName( );

			if ( m_strLogFilePath!=strFileName )
			{
				mode = wxFile::write;

				if ( m_strLogFilePath.empty() )
				{
					wxFileName fn(strFileName);
					wxDateTime dtAccess, dtMod, dtCreate;

					if ( fn.FileExists() &&
						 fn.GetTimes(&dtAccess, &dtMod, &dtCreate) )
					{
						wxDateTime dt = wxDateTime::Now();

						if ( dt.IsSameDate(dtMod) )
						{
							mode = wxFile::write_append;
						}
					}
				}

				m_strLogFilePath=strFileName;
			}

			wxFile theLog( strFileName, mode );

			if ( theLog.IsOpened() )
			{
				wxFileOffset lLength = theLog.Length();

				unsigned long lLimit = m_lLimit;
				lLimit*=1024*1024;

				if ( lLimit>0  && lLength>lLimit )
				{
					//Truncate file
					theLog.Close();
					if (!theLog.Open( strFileName, wxFile::write ) )
					{
						throw 1;
					}

					LOGENTRY entry;
					entry.strCustomEvent = m_strName;
					entry.strCustomEvent+= wxT(":");
					entry.strCustomEvent+= strFileName;
					entry.strCustomEvent+= wxT(" ");
					entry.strCustomEvent+=_(" file exceeds the limit and will be truncated.");
					m_guiLogic.AddToLog(entry);
				}

				theLog.Write( strLogRec );

				#ifdef WIN32
				theLog.Write( wxT("\r\n") );
				#else
				theLog.Write( wxT("\n") );
				#endif
			}
			else
			{
				ReportSaveFailed(strFileName);
			}

			theLog.Close();
		}
		catch(...)
		{
			ReportSaveFailed(strFileName);
		}
	}
}

void CLogSaver::ReportSaveFailed(const wxString& strFileName)
{
	LOGENTRY entry;
	entry.bDoNotSave = true;
	entry.strCustomEvent = m_strName;
	entry.strCustomEvent+= wxT(":");
	entry.strCustomEvent+= strFileName;
	entry.strCustomEvent+= wxT(" ");
	entry.strCustomEvent+=_(" save failed.");
	m_guiLogic.AddToLog(entry);
}

wxString CLogSaver::GenerateFileName()
{
	wxDateTime dt = wxDateTime::Now();
	wxDateTime::WeekDay wd = dt.GetWeekDay();
	wxString wkName = wxDateTime::GetWeekDayName(wd);

	wxFileName fn( m_strPath, wkName, wxT("log") );

	if ( !fn.DirExists() )
	{
		try
		{
			fn.Mkdir( 511, wxPATH_MKDIR_FULL );
		}
		catch(...)
		{
		}
	}

	return fn.GetFullPath() ;
}

bool CLogSaver::IsSaveEnabled()
{
	bool bRes(false);

	if ( !m_strPath.empty() )
	{
		wxFileName fn( m_strPath );

		bRes = fn.IsOk();
	}

	return bRes;
}
